package org.blog.service;

import java.util.List;

import org.blog.dao.PassportDao;
import org.blog.helper.Constants;
import org.blog.helper.Result;
import org.blog.model.ActionType;
import org.blog.model.ModelPassport;
import org.blog.service.util.ServiceConstants;
import org.blog.service.util.ValidateHelper;
import org.blog.util.encode.PwdEncode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class PassportServiceImpl implements PassportService {

	@Autowired
	private PassportDao passportDao;

	public Result<?> addUser(ModelPassport t) {
		if (ValidateHelper.validate(t, ActionType.ADD)) {

			t.setCreateTime(System.currentTimeMillis() / 1000);
			t.setRole(ServiceConstants.COMMENTS);
			String randKey = PwdEncode.getRandKey();
			t.setSalt(randKey);
			t.setPwd(PwdEncode.getEncryptPassword(t.getPwd(), randKey));

			passportDao.addUser(t);
			return new Result<Object>(200);
		} else {
			return new Result<Object>(400);
		}
	}
	
	@Autowired
	public Result<?> getUserByEmail(ModelPassport t){
		if (ValidateHelper.validate(t, ActionType.AUTH)) {
			ModelPassport passport=passportDao.getUserByEmail(t.getEmail());
			return new Result<Object>(passport,200);
		} else {
			return new Result<Object>(400);
		}
	}
	
	@Autowired
	public Result<?> getUserByUsername(ModelPassport t){
		if (ValidateHelper.validate(t, ActionType.AUTH)) {
			ModelPassport passport=passportDao.getUser(t.getUsername());
			return new Result<Object>(passport,200);
		} else {
			return new Result<Object>(400);
		}
	}

	@Override
	public Result<?> auth(ModelPassport t) {

		if (ValidateHelper.validate(t, ActionType.AUTH)) {
			if (t.getRole() >= ServiceConstants.CHECK) {
				ModelPassport enPass = passportDao.getUser(t.getUsername());
				if (enPass == null) {
					return new Result<Object>(404);
				}
				String pwd = PwdEncode.getUnEncryptPassword(enPass.getPwd(),
						enPass.getSalt());
				if (pwd.equals(t.getPwd())) {
					return new Result<Object>(200);
				} else {
					return new Result<Object>(404);
				}
			} else {
				return new Result<Object>(401);
			}
		} else {
			return new Result<Object>(400);
		}
	}

	@Override
	public Result<?> modifyEmail(ModelPassport t, ModelPassport passport) {
		if (ValidateHelper.validate(t, ActionType.AUTH)) {
			t.setUsername(passport.getUsername());
			passportDao.modifyEmail(t);
			return new Result<Object>(200);
		} else {
			return new Result<Object>(400);
		}
	}

	@Override
	public Result<?> modifyPassport(ModelPassport t, ModelPassport passport) {
		if (ValidateHelper.validate(t, ActionType.AUTH)) {
			t.setUsername(passport.getUsername());
			String randKey = PwdEncode.getRandKey();
			t.setSalt(randKey);
			t.setPwd(PwdEncode.getEncryptPassword(t.getPwd(), randKey));
			passportDao.modifyPassport(t);

			return new Result<Object>(200);
		} else {
			return new Result<Object>(400);
		}
	}

	@Override
	public Result<List<ModelPassport>> getLoginList(int page,
			ModelPassport passport) {
		if (passport != null && passport.getRole() >= ServiceConstants.ADMIN) {
			int start = (page - 1) * Constants.pages;
			int end = page * Constants.pages;
			List<ModelPassport> list = passportDao.getLoginList(start, end);
			return new Result<List<ModelPassport>>(list, 200);
		} else {
			return new Result<List<ModelPassport>>(401);
		}
	}

	@Override
	public Result<?> modifyRole(ModelPassport t, ModelPassport passport) {
		if (passport != null && passport.getRole() >= ServiceConstants.ADMIN) {
			if (t == null || "root".equals(t.getUsername())
					|| t.getRole() == null) {
				return new Result<Object>(400);
			}
			passportDao.modifyRole(t);

			return new Result<Object>(200);
		} else {
			return new Result<Object>(401);
		}
	}
}
